home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 003 / dbapg.arc / FIX.ASM < prev    next >
Encoding:
Assembly Source File  |  1984-08-12  |  5.6 KB  |  282 lines

  1. ; Subroutine.: FIX.ASM
  2. ; Author.....: Bob Doolittle
  3. ; Date.......: 08/12/84
  4. ; Notice.....: Copyright 1984, Ashton-Tate, All Rights Reserved.
  5. ; Notes......: CP/M 80 version.
  6. ;
  7.     ORG    100H
  8. ;
  9. ;BDOS equates
  10. ;
  11. SYSTEM    EQU    0        ;System entry point
  12. BDOS    EQU    5        ;BDOS entry point
  13. TBUF    EQU    80H        ;Default buffer
  14. FCB    EQU    5CH        ;Default FCB
  15. CFS    EQU    35        ;Compute file size
  16. CONIN    EQU    1        ;Console in
  17. CONOUT    EQU    2        ;Console out
  18. PSTR    EQU    9        ;Print string
  19. OPEN    EQU    15        ;Open file
  20. CLOSE    EQU    16        ;Close file
  21. READ    EQU    20        ;Read sequential
  22. WRITE    EQU    21        ;Write sequential
  23. MAKE    EQU    22        ;Make file
  24. DELETE    EQU    19        ;Delete file
  25. READR    EQU    33        ;Read random
  26. ;
  27. ;ASCII equates
  28. ;
  29. CR    EQU    13
  30. LF    EQU    10
  31. ;
  32. START:    XRA    A        ;Initialize CRLF flag to destroy CRLF
  33.     STA    FLG
  34.     LDA    FCB+1
  35.     CPI    20H        ;Was input file on command line
  36.     JNZ    ST2        ;Yes
  37.     LXI    D,MES1        ;No, print message and abort
  38.     MVI    C,PSTR
  39.     CALL    BDOS
  40.     JMP    EXIT
  41. ;
  42. ST2:    LXI    D,MES4        ;Print CRLF message
  43.     MVI    C,PSTR
  44.     CALL    BDOS
  45.     MVI    C,CONIN        ;Get response
  46.     CALL    BDOS
  47.     ANI    5FH        ;Make upper case
  48.     CPI    'Y'
  49.     JNZ    ST7
  50.     MVI    A,1
  51.     STA    FLG
  52. ST7:    MVI    C,PSTR        ;Ask for # of bytes
  53.     LXI    D,MES6
  54.     CALL    BDOS
  55.     LXI    D,CBUF        ;Get response
  56.     MVI    C,10
  57.     CALL    BDOS
  58.     LXI    H,CBUF+2
  59.     CALL    CONV        ;Convert to binary in HL
  60.     CALL    DIV        ;Divide by 128
  61.     SHLD    QUO        ;Save quotient and remainder
  62.     CALL    CRLF
  63.     CALL    CLEAR        ;Clear FCB+32-35, FCB+15 and FCB+12
  64.     LXI    H,FCB
  65.     LXI    D,FCB1
  66.     MVI    C,9
  67. MOVE:    MOV    A,M        ;Move file name and drive from FCB to FCB1
  68.     STAX    D
  69.     INX    H
  70.     INX    D
  71.     DCR    C
  72.     JNZ    MOVE
  73.     LXI    D,FCB
  74.     MVI    C,OPEN        ;Open file
  75.     CALL    BDOS
  76.     INR    A        ;Was open successful?
  77.     JNZ    ST3        ;Yes
  78.     LXI    D,MES2        ;No, print message and abort
  79.     MVI    C,PSTR
  80.     CALL    BDOS
  81.     JMP    EXIT
  82. ST3:    LXI    D,MES5        ;Working message
  83.     MVI    C,PSTR
  84.     CALL    BDOS
  85.     LXI    D,FCB
  86.     MVI    C,CFS        ;Compute file size
  87.     CALL    BDOS
  88.     LHLD    FCB+33        ;Get last record+1
  89.     SHLD    REC        ;Save locally
  90.     DCX    H        ;Last record number
  91.     SHLD    FCB+33        ;Store it back
  92.     MVI    C,READR
  93.     LXI    D,FCB
  94.     CALL    BDOS        ;Read last record
  95.     LXI    H,TBUF-1
  96.     MVI    C,0        ;Initialize counter
  97. LOP:    INX    H        ;Scan to 1st EOF mark and keep count
  98.     INR    C
  99.     MOV    A,M        ;Get byte
  100.     CPI    1AH        ;Is it EOF?
  101.     JNZ    LOP        ;No, keep looping
  102.     PUSH    B        ;Save count to EOF in last record
  103.     CALL    CLEAR        ;Clear again
  104.     LXI    D,FCB1
  105.     MVI    C,DELETE    ;Delete any possible previous out file
  106.     CALL    BDOS
  107.     LXI    D,FCB
  108.     MVI    C,OPEN        ;Re-open the input file
  109.     CALL    BDOS
  110.     LXI    D,FCB1
  111.     MVI    C,MAKE        ;Make output file
  112.     CALL    BDOS
  113. ;
  114. FIRS:    LXI    D,FCB        ;Scan off quotient records
  115.     MVI    C,READ
  116.     CALL    BDOS
  117.     ORA    A
  118.     JZ    FST1        ;Read OK
  119.     LXI    D,MES3
  120.     MVI    C,PSTR
  121.     CALL    BDOS
  122.     POP    B        ;Balance stack
  123.     JMP    EXIT
  124. ;
  125. FST1:    LHLD    REC        ;Bump record count down
  126.     DCX    H
  127.     SHLD    REC
  128.     LHLD    QUO        ;Get DIV result
  129.     MOV    A,H
  130.     ORA    A        ;Have we read sector with remainder ?
  131.     JZ    FST2        ;Yes
  132.     DCR    H        ;No
  133.     SHLD    QUO
  134.     LXI    D,FCB1        ;Write sector
  135.     MVI    C,WRITE
  136.     CALL    BDOS
  137.     JMP    FIRS
  138. FST2:    MVI    A,80H
  139.     ADD    L
  140.     MOV    L,A        ;HL now has filter start address
  141. FST3:    CALL    FILTER
  142.     INX    H
  143.     MOV    A,H
  144.     ORA    A
  145.     JZ    FST3        ;Filter to end of sector
  146.     LXI    D,FCB1
  147.     MVI    C,WRITE        ;Write to output file
  148.     CALL    BDOS
  149. LOOP:    LXI    D,FCB
  150.     MVI    C,READ        ;Read a record to default buffer
  151.     CALL    BDOS
  152.     ORA    A        ;Was read successful?
  153.     JZ    LO4        ;Yes
  154. ERR:    LXI    D,MES3
  155.     MVI    C,PSTR        ;No, print message and abort
  156.     CALL    BDOS
  157.     POP    B        ;Balance stack
  158.     JMP    EXIT
  159. ;
  160. LO4:    LHLD    REC        ;Get record count
  161.     DCX    H        ;Decrement record count
  162.     SHLD    REC        ;Save it
  163.     MOV    A,H
  164.     ORA    L        ;Are we at last record?
  165.     JNZ    GOAH        ;No
  166.     POP    B        ;Yes, get back count
  167.     LXI    H,TBUF
  168. ;
  169. LO1:    DCR    C        ;Are we at EOF
  170.     JZ    LO2        ;Yes
  171.     CALL    FILTER
  172.     INX    H
  173.     JMP    LO1        ;Loop
  174. ;
  175. LO2:    LXI    D,FCB1
  176.     MVI    C,WRITE        ;Write last record
  177.     CALL    BDOS
  178.     LXI    D,FCB1
  179.     MVI    C,CLOSE        ;Close output file
  180.     CALL    BDOS
  181. EXIT:    JMP    SYSTEM        ;Finished
  182. ;
  183. GOAH:    LXI    H,TBUF
  184.     MVI    C,80H        ;Initialize record count
  185. GO1:    CALL    FILTER        ;Scan the record
  186.     INX    H
  187.     DCR    C        ;End of record?
  188.     JNZ    GO1        ;No
  189.     LXI    D,FCB1
  190.     MVI    C,WRITE        ;Write record to output file
  191.     CALL    BDOS
  192.     JMP    LOOP        ;Loop to read next record
  193. ;
  194. FILTER: MOV    A,M
  195.     ANI    7FH        ;Strip bit 7
  196.     MOV    M,A
  197.     CPI    20H        ;Is it < space
  198.     RNC            ;No
  199.     MOV    B,A        ;Save byte
  200.     LDA    FLG        ;Get CRLF flag
  201.     ORA    A        ;Are we killing CRLF ?
  202.     JZ    FIL1        ;Yes
  203.     MOV    A,B        ;No, get byte in A
  204.     CPI    CR
  205.     RZ            ;Don't change
  206.     CPI    LF
  207.     RZ            ;Don't change
  208. FIL1:    MVI    M,20H        ;Replace anything < space by space
  209.     RET
  210. ;
  211. CONV:    LXI    D,0        ;Convert # pointed to by HL to binary in HL
  212.     XCHG            ;Conversion stops when # < '0' is found
  213. CONV1:    LDAX    D
  214.     SUI    '0'
  215.     RM
  216.     CPI    10
  217.     CMC
  218.     RC
  219.     INX    D
  220.     DAD    H
  221.     PUSH    H
  222.     DAD    H
  223.     DAD    H
  224.     POP    B
  225.     DAD    B
  226.     MOV    C,A
  227.     MVI    B,0
  228.     DAD    B
  229.     JMP    CONV1
  230. ;
  231. DIV:    MVI    B,0FFH        ;Divide by 128
  232.     LXI    D,-128        ;Quotient in H, remainder in L
  233. DIV1:    DAD    D
  234.     INR    B
  235.     MOV    A,H
  236.     ORA    A
  237.     JP    DIV1
  238.     LXI    D,128
  239.     DAD    D
  240.     MOV    H,B
  241.     RET
  242. ;
  243. CLEAR:    XRA    A        ;Zero key fields in FCB
  244.     LXI    H,FCB+32
  245.     MOV    M,A
  246.     INX    H
  247.     MOV    M,A
  248.     INX    H
  249.     MOV    M,A
  250.     INX    H
  251.     MOV    M,A
  252.     STA    FCB+15
  253.     STA    FCB+12
  254.     RET
  255. CRLF:    MVI    E,CR
  256.     MVI    C,CONOUT
  257.     CALL    BDOS
  258.     MVI    E,LF
  259.     MVI    C,CONOUT
  260.     CALL    BDOS
  261.     RET
  262. MES1:    DB    CR,LF,'    USAGE: FIX [d:]<filename.typ>'
  263.     DB    CR,LF,'where d is an optional drive designator'
  264.     DB    CR,LF,'and filename.typ is the file name and type'
  265.     DB    CR,LF,'of the file to be processed. The output'
  266.     DB    CR,LF,'file will be named filename.FIX.$'
  267. MES2:    DB    CR,LF,'Cannot find file on specified drive.$'
  268. MES3:    DB    CR,LF,'Read past EOF.$'
  269. MES4:    DB    CR,LF,'Do you want to preserve CRLF''s (Y or N) ? $'
  270. MES5:    DB    CR,LF,'Working...$'
  271. MES6:    DB    CR,LF,'What is the size of the header (in bytes)? $'
  272. CBUF:    DB    10,0
  273.     DB    0,0,0,0,0,0,0,0,0,0,0,0
  274. REC    DS    2        ;Storage for record count
  275. QUO:    DS    2        ;Storage for quotient and remainder
  276. FLG:    DB    0        ;CRLF flag
  277. FCB1:    DB    0,'        FIX'
  278.     REPT    24
  279.     DB    0
  280.     ENDM
  281.  
  282.